﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.popups.missions {
    import reprise.ui.*;
    import flash.events.*;
    import reprise.events.*;
    import flash.display.*;
    import reprise.controls.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.world.vos.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.views.popups.*;
    import net.wooga.pets.views.components.*;
    import net.wooga.pets.constants.*;
    import net.wooga.pets.utils.*;

    public class AbstractMissionPopupMediator extends AbstractPopupMediator {

        private const GIVE_BUTTON_PADDING:int = 16;

        public var computerModel:ComputerModel;
        protected var _mission:MissionVO;
        protected var _speakerLinkage:String = "MissionSpeechBubble"

        override public function onRegister():void{
            super.onRegister();
            this._mission = MissionVO(this.view.data);
            this.setIcon();
            this.setHeader();
            this.setContent();
            this.setStory();
        }
        override protected function onOk(_arg1:MouseEvent):void{
            super.onOk(_arg1);
            var _local2:Object = (_arg1.target as LabelButton).data();
            if (_local2){
                dispatch(new InfoEvent(_local2.type, _local2.id));
            };
        }
        private function setIcon():void{
            var clip:MovieClip = AssetsLibrary.getMovieClip((this._mission.code + "icon"));
            var comp:ClipComponent = RepriseUtils.addClipComponent(this.view, "missionIcon", clip);
            RepriseUtils.validateComponent(comp, function (_arg1:ClipComponent):void{
                Displays.correctScaling(_arg1, _arg1.clip, _arg1.width, _arg1.height, -1, -1);
            });
        }
        private function setHeader():void{
            var _local1:String = language.getTextByKey("mission_general_label");
            var _local2:String = language.getTextByKey((("mission_" + this._mission.code) + "_title"));
            RepriseUtils.addLabel(this.view, "missionHeader myriadProFontOutline", ((_local1 + " ") + _local2));
        }
        protected function addGiveButton(_arg1:TaskVO, _arg2:Boolean, _arg3:UIComponent=null):void{
            var _local4:String = this.getGiveButtonKey(_arg1.type);
            var _local5:String = (_arg2) ? "greenButton" : "greyButton";
            var _local6:LabelButton = createButton(_arg3, ("sendMayorButton " + _local5), _local4, "myriadProFont", {amount:_arg1.required});
            _local6.setData(_arg1);
            _local6.enabled = _arg2;
            if (_arg2){
                eventMap.mapListener(_local6, MouseEvent.CLICK, this.onGive);
            };
            _local6.addEventListener(DisplayEvent.VALIDATION_COMPLETE, this.onButtonValidated);
        }
        private function onButtonValidated(_arg1:DisplayEvent):void{
            var _local2:LabelButton = (_arg1.target as LabelButton);
            _local2.removeEventListener(DisplayEvent.VALIDATION_COMPLETE, this.onButtonValidated);
            var _local3:Label = (_local2.getElementBySelector("Label") as Label);
            var _local4:int = (_local3.textWidth + this.GIVE_BUTTON_PADDING);
            _local2.setStyle("width", _local4.toString());
        }
        private function getGiveButtonKey(_arg1:String):String{
            var _local2 = "global_button_give_";
            switch (_arg1.toLowerCase()){
                case GameConstants.LOVE:
                case GameConstants.FIRE:
                case GameConstants.WATER:
                case GameConstants.AIR:
                case GameConstants.STAR:
                case GameConstants.ICE:
                    return ((_local2 + "gems"));
            };
            return ((_local2 + _arg1.toLowerCase()));
        }
        protected function onGive(_arg1:MouseEvent):void{
        }
        protected function setContent():void{
            var _local2:Object;
            var _local1 = "global_button_ok";
            switch (true){
                case this.hasMissionOfType(GameConstants.GET_ACHIEVEMENT):
                    _local1 = "global_open_achievements";
                    _local2 = {type:EventConstants.CREATE_POPUP, id:GameConstants.ACHIEVEMENT_BOOK_POPUP};
                    break;
                case this.hasMissionOfType(GameConstants.CURE_PET):
                    _local1 = "global_open_computer";
                    _local2 = {type:EventConstants.SHOW_PATIENT_POOL_COMMAND, id:null};
                    break;
            };
            this.setOkButton(_local1, _local2);
        }
        protected function setOkButton(_arg1:String, _arg2=null):void{
            var _local3:LabelButton = this.createOkButton(_arg1);
            eventMap.mapListener(_local3, MouseEvent.CLICK, this.onOk);
            _local3.setData(_arg2);
        }
        private function setStory():void{
            var _local1:MovieClip = AssetsLibrary.getMovieClip(this._speakerLinkage);
            var _local2:ClipComponent = RepriseUtils.addClipComponent(this.view, "missionStory", _local1);
            var _local3:String = ("mission_" + this._mission.code);
            _local3 = (_local3 + (this._mission.isSolved) ? "_reward_message" : "_story");
            addLabel("missionStoryLabel myriadProFontOutline", _local3, null, _local2);
        }
        override protected function createOkButton(_arg1:String, _arg2:UIComponent=null):LabelButton{
            return (createButton(_arg2, "greenButton", _arg1, "myriadProFont"));
        }
        private function get view():AbstractMissionPopupView{
            return ((viewComponent as AbstractMissionPopupView));
        }
        protected function hasMissionOfType(_arg1:String):Boolean{
            var _local2:TaskVO;
            for each (_local2 in this._mission.tasks) {
                if ((((_local2.action == _arg1)) && (!(_local2.isFinished)))){
                    return (true);
                };
            };
            return (false);
        }
        protected function getTaskThumb(_arg1:TaskVO):String{
            if (_arg1.type == GameConstants.ANY_TYPE){
                return (_arg1.action);
            };
            if (_arg1.action == GameConstants.GET_ACHIEVEMENT){
                return (_arg1.type.slice(0, _arg1.type.lastIndexOf("_")));
            };
            return (_arg1.type);
        }
        protected function getTaskIcon(_arg1:String):MovieClip{
            return (((((((AssetsLibrary.getMovieClip((GameConstants.CUSTOMIZE_ICON_PREFIX + _arg1))) || (AssetsLibrary.getMovieClip((_arg1 + "Thumb"))))) || (AssetsLibrary.getMovieClip((("Reward" + _arg1) + "Thumb"))))) || (AssetsLibrary.getMovieClip(("RC_Reward" + _arg1)))));
        }
        protected function getPetName(_arg1:String):String{
            var _local2:String = this.computerModel.getPatientNameByDisease(_arg1);
            var _local3:String = (_local2) ? language.getTextByKey(("pet_" + _local2.toLowerCase()), false) : "";
            return (_local3);
        }

    }
}//package net.wooga.pets.views.popups.missions 
